package top.yonyong.myprometheus;

import io.micrometer.core.instrument.MeterRegistry;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import top.yonyong.myprometheus.prometheus.PrometheusMetricsInterceptor;

/**
 * @author admin
 */
@SpringBootApplication
@EnableSwagger2
public class Application extends WebMvcConfigurationSupport implements CommandLineRunner{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) {
        DefaultExports.initialize();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("*** JVM stop ***")));
    }

    @Bean(value = "meterRegistryCustomizer")
    MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer() {
        return meterRegistry -> meterRegistry.config()
                .commonTags("application", "springboot-app");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PrometheusMetricsInterceptor()).addPathPatterns("/**");
    }
}
